home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex3.c < prev    next >
C/C++ Source or Header  |  1995-06-04  |  3KB  |  75 lines

  1. #include <genstub.c>
  2.  
  3. //  Child thread procedure waits until mutex becomes signaled, holds the
  4. // object for five seconds and then releases it.
  5.  
  6. DWORD WINAPI ChildThreadProc( HWND hWnd )
  7. {
  8.     char szBuffer[128];
  9.     HANDLE hMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, "EXAMPLE_MUTEX" );
  10.     wsprintf( szBuffer,"Thread %x waiting for Mutex",
  11.                  GetCurrentThreadId( ));
  12.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  13.     // Wait for signaled mutex.
  14.     WaitForSingleObject( hMutex, INFINITE );
  15.     wsprintf( szBuffer,"Thread %x got mutex!", GetCurrentThreadId( ) );
  16.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  17.     Sleep( 5000 ); // Shut out other threads.
  18.     // Release mutex.
  19.     wsprintf( szBuffer,"Thread %x is done with mutex", GetCurrentThreadId( ) );
  20.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  21.     ReleaseMutex( hMutex );
  22.     CloseHandle( hMutex );
  23.     ExitThread( TRUE );
  24. }
  25.  
  26. // Windows message procedure.
  27. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  28. {
  29.    static HANDLE hMutex = 0;
  30.  
  31.    switch (uMsg) {
  32.            case WM_CREATE:
  33.                    hMutex = CreateMutex( NULL, FALSE, "EXAMPLE_MUTEX" );
  34.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  35.            case WM_COMMAND:       // process menu items
  36.                    switch ( LOWORD( wParam )  )
  37.                    {
  38.                       case IDM_TEST: // Make a thread
  39.                         {
  40.                            DWORD id;
  41.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &id );
  42.                         }
  43.                       break;
  44.                       case IDM_EXIT:
  45.                            DestroyWindow( hWnd );
  46.                            break;
  47.                    }
  48.            break;
  49.            case WM_USER:
  50.                    {  // Message to show synchronization actions.
  51.                       TCHAR szBuffer[101];
  52.                       static int row = 0;
  53.                       static int msg_num = 1;
  54.                       HDC hDC = GetDC( hWnd );
  55.                       FillMemory( szBuffer, 100, 32 );
  56.                       TextOut( hDC, 0, row, szBuffer, 100 );
  57.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  58.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  59.                       if ( row > 200 )
  60.                          row = 0;
  61.                       else
  62.                          row += 20;
  63.                       ReleaseDC( hWnd, hDC );
  64.                    }
  65.                    break;
  66.            case WM_DESTROY:
  67.                    if ( hMutex )
  68.                       CloseHandle( hMutex );
  69.                    PostQuitMessage( 0 );
  70.                    break;
  71.            default:
  72.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  73.    }
  74.    return NULL;
  75. }